package com.unicom.priceboard.thread;


import com.unicom.priceboard.data.hose.MarketStatusReader;
import com.unicom.priceboard.util.Constants;
import com.unicom.priceboard.util.DataPath;
import com.unicom.priceboard.util.ReadFileUtil;
import com.unicom.priceboard.util.Util;

public class MarketStatusThread implements Runnable{
	private int wait=1;
	private boolean flag=true;
	
	private static String marketStaSrctPath = "";
	
	public static char prevStat;
	public static char curStat;
	
	public MarketStatusThread()
	{
		marketStaSrctPath = DataPath.basePath + DataPath.marketStaSrctPath;
		this.wait=Integer.parseInt(DataPath.hoseWait);
	}
	
	public void run() {
		while(true)	{
			try{				
				if((TimerThread.CURRENT_DAY_OF_WEEK!=1 && TimerThread.CURRENT_DAY_OF_WEEK!=7)||flag){
					if(TimerThread.CURRENT_TIME<=Constants.READING_TIME_END) {	
						flag=false;
						//ReadFileUtil checkmodify =new ReadFileUtil(Util.getCurrentDataPath(marketStaSrctPath));
						if(ReadFileUtil.isUpToDate(Util.getCurrentDataPath(marketStaSrctPath))){
							 MarketStatusReader ReadData =new MarketStatusReader();
							 ReadData.MarketReader(marketStaSrctPath);
							
						 }//end check modify
					}
				}
			}catch(Exception e)	{
				//logger.debug("*****parsing TotalMarket process error :\n"+e.getMessage());
			}			
//			wait a period before continue to read
			try {
				Thread.sleep(1000*wait);
			} catch (Exception e) {
				//logger.debug("Exception: " + e.getMessage());
			}
		}

	}//end run
	/***************method get market satus********************/
	public static String getCurrentMarketState() {
		//default is inactive state
		String val = Constants.MARKET_STATE_INACTIVE;		
		//market closes in Saturday and Sunday
		if(TimerThread.CURRENT_DAY_OF_WEEK==1 || TimerThread.CURRENT_DAY_OF_WEEK==7)
			return val;
		
		if(curStat==Constants.MARKET_SESSION1_START)
			return Constants.MARKET_STATE_OPEN_SESSION;
		else if(prevStat==Constants.MARKET_SESSION1_START && curStat==Constants.MARKET_SESSION2_START) {
			if(TotalMarketThread.mapTotalMarket.get(1)!=null)
				return Constants.MARKET_STATE_CONTINOUS_SESSION;
			else return Constants.MARKET_STATE_OPEN_SESSION;
		}else if(prevStat==Constants.MARKET_SESSION2_START && curStat==Constants.MARKET_SESSION3_START)
				return Constants.MARKET_STATE_CLOSE_SESSION;
		else if(prevStat==Constants.MARKET_SESSION3_START && curStat==Constants.MARKET_SESSION4_START) {
			if(TotalMarketThread.mapTotalMarket.get(3)!=null)
				return Constants.MARKET_STATE_PTD_SESSION;
			else return Constants.MARKET_STATE_CLOSE_SESSION;
		}else if(prevStat==Constants.MARKET_SESSION4_START && curStat==Constants.MARKET_SESSION4_END)
			return Constants.MARKET_STATE_END_TRADING;
		
		if(marketStaSrctPath!=null && marketStaSrctPath!=""){
			if(ReadFileUtil.isUpToDate(Util.getCurrentDataPath(marketStaSrctPath))){
				if(TimerThread.CURRENT_TIME < 835)
					return Constants.MARKET_STATE_PRE_OPEN;
			}
		}
		
		if(TimerThread.CURRENT_TIME>=Constants.READING_TIME_END)
			return Constants.MARKET_STATE_END_TRADING;
		else
			return val;
	}
}
